1 Mapping the Lower Layer Super Output Area (LSOA)

1.1 Mapping Domestic

1.1.1 Mapping Electricity consumption

####2019

#Electricty consumption data LSOA pre downloaded
inFile <-paste0(dataFolder, "energy/electricity/LSOA Dom Elec csv/LSOA_ELEC_2019.csv")
lsoa_elecData <- readr::read_csv(inFile)
## 
## -- Column specification --------------------------------------------------------
## cols(
##   `Local Authority Name` = col_character(),
##   `Local Authority Code` = col_character(),
##   `Middle Layer Super Output Area (MSOA) Name` = col_character(),
##   `Middle Layer Super Output Area (MSOA) Code` = col_character(),
##   `Lower Layer Super Output Area (LSOA) Name` = col_character(),
##   `Lower Layer Super Output Area (LSOA) Code` = col_character(),
##   `Total number of domestic electricity meters` = col_double(),
##   `Total domestic electricity consumption (kWh)` = col_double(),
##   `Mean domestic electricity consumption 
## (kWh per meter)` = col_double(),
##   `Median domestic electricity consumption 
## (kWh per meter)` = col_double()
## )
lsoa_elecData$LSOA11CD <- lsoa_elecData$`Lower Layer Super Output Area (LSOA) Code`
head(lsoa_elecData)
## # A tibble: 6 x 11
##   `Local Authority~ `Local Authority~ `Middle Layer Super ~ `Middle Layer Super~
##   <chr>             <chr>             <chr>                 <chr>               
## 1 Hartlepool        E06000001         Hartlepool 001        E02002483           
## 2 Hartlepool        E06000001         Hartlepool 001        E02002483           
## 3 Hartlepool        E06000001         Hartlepool 001        E02002483           
## 4 Hartlepool        E06000001         Hartlepool 001        E02002483           
## 5 Hartlepool        E06000001         Hartlepool 001        E02002483           
## 6 Hartlepool        E06000001         Hartlepool 001        E02002483           
## # ... with 7 more variables: Lower Layer Super Output Area (LSOA) Name <chr>,
## #   Lower Layer Super Output Area (LSOA) Code <chr>,
## #   Total number of domestic electricity meters <dbl>,
## #   Total domestic electricity consumption (kWh) <dbl>,
## #   Mean domestic electricity consumption  (kWh per meter) <dbl>,
## #   Median domestic electricity consumption  (kWh per meter) <dbl>,
## #   LSOA11CD <chr>
#The LSOA boundaries for the Solent have been pre-downloaded
inf<-paste0(dataFolder, "boundaries/LSOA/lsoa_solent.shp")
message ("Loading LSOA boundaries from file")
## Loading LSOA boundaries from file
lsoa_sf_data <- sf::read_sf(inf)
head(lsoa_sf_data)
## Simple feature collection with 6 features and 15 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 465991.4 ymin: 100849 xmax: 467770.5 ymax: 102360.8
## Projected CRS: OSGB 1936 / British National Grid
## # A tibble: 6 x 16
##   LSOA11CD  OBJECTID LSOA11NM   LSOA11NMW    BNG_E  BNG_N  LONG   LAT Shape__Are
##   <chr>        <int> <chr>      <chr>        <int>  <int> <dbl> <dbl>      <dbl>
## 1 E01017013    16519 Portsmout~ Portsmouth~ 466127 101741 -1.06  50.8    145134.
## 2 E01017014    16520 Portsmout~ Portsmouth~ 466924 101957 -1.05  50.8    757371.
## 3 E01017015    16521 Portsmout~ Portsmouth~ 467084 101499 -1.05  50.8    472647.
## 4 E01017016    16522 Portsmout~ Portsmouth~ 466330 101597 -1.06  50.8    122927.
## 5 E01017017    16523 Portsmout~ Portsmouth~ 466813 101069 -1.05  50.8    166924.
## 6 E01017018    16524 Portsmout~ Portsmouth~ 466350 101184 -1.06  50.8    184945.
## # ... with 7 more variables: Shape__Len <dbl>, MSOA11CD <chr>, MSOA11NM <chr>,
## #   LAD11CD <chr>, LAD11NM <chr>, nLSOAs <int>, geometry <MULTIPOLYGON [m]>
table(lsoa_sf_data$LAD11NM)#How many LSOAs are there in each LA
## 
## Basingstoke and Deane        East Hampshire             Eastleigh 
##                   109                    72                    77 
##               Fareham               Gosport                  Hart 
##                    73                    53                    57 
##                Havant         Isle of Wight            New Forest 
##                    78                    89                   114 
##            Portsmouth           Southampton           Test Valley 
##                   125                   148                    71 
##            Winchester 
##                    70
lsoa_merged_sf <- merge(lsoa_sf_data, lsoa_elecData) #merging the boundaries and energy data
ggplot2::ggplot(lsoa_merged_sf) + 
  geom_sf(aes(fill = `Mean domestic electricity consumption \n(kWh per meter)`)) +
  scale_fill_continuous(name = "Mean kWh per meter", low = "green", high = "red") +
  labs(caption = "Solent (all LSOAs)")

#Mapping it

####Map with Overlay

library(leaflet)

st_coord_sys <- st_crs(lsoa_merged_sf)
st_coord_sys$epsg
## [1] 27700
#Trasforming the coord systen
if(st_coord_sys$epsg !=4326){
map_df_trans <- st_transform(lsoa_merged_sf, "+proj=longlat +datum=WGS84")
}
qpal <- colorNumeric("Reds", map_df_trans$`Mean domestic electricity consumption \n(kWh per meter)`, n=9)

leaflet(map_df_trans) %>%
addTiles() %>%  # Add default OpenStreetMap map tiles
  addPolygons(color = ~qpal(`Mean domestic electricity consumption \n(kWh per meter)`),
              fillOpacity = 0.6, weight = 1.5, popup = ~(LSOA11CD), 
             
  
               highlight = highlightOptions(
                weight = 5,
                color = "#666",
                fillOpacity = 0.7,
                bringToFront = TRUE))
1.1.1.0.1 Looking at cities in more detail
#Cities such as Southampton disappear due to their density, we will now map Southampton alone to see this area in more detail
mapData <- dplyr::filter(lsoa_merged_sf, LAD11NM == "Southampton")
# plotting the electricity consumption of Southampton in more detail
ggplot2::ggplot(mapData) + 
  geom_sf(aes(fill = `Mean domestic electricity consumption \n(kWh per meter)`)) +
  scale_fill_continuous(name = "Mean kWh per meter", low = "green", high = "red") +
  labs(caption = "Southampton")

#Cities such as Portsmouth disappear due to their density, we will now map Portsmouth alone to see this area in more detail
mapData <- dplyr::filter(lsoa_merged_sf, LAD11NM == "Portsmouth")
# plotting the electricity consumption of Portsmouth in more detail
ggplot2::ggplot(mapData) + 
  geom_sf(aes(fill = `Mean domestic electricity consumption \n(kWh per meter)`)) +
  scale_fill_continuous(name = "Mean kWh per meter", low = "green", high = "red") +
  labs(caption = "Portsmouth")

#Cities such as Winchester disappear due to their density, we will now map Winchester alone to see this area in more detail
mapData <- dplyr::filter(lsoa_merged_sf, LAD11NM == "Winchester")

# plotting the electricity consumption of Winchester in more detail
ggplot2::ggplot(mapData) + 
  geom_sf(aes(fill = `Mean domestic electricity consumption \n(kWh per meter)`)) +
  scale_fill_continuous(name = "Mean kWh per meter", low = "green", high = "red") +
  labs(caption = "Winchester")

####Difference between 2010 and 2019 dom elec

domelec_LSOA_2010 <- readr::read_csv(paste0(dataFolder,"energy/electricity/LSOA Dom Elec csv/LSOA_ELEC_2010.csv"))
## 
## -- Column specification --------------------------------------------------------
## cols(
##   `Local Authority Name` = col_character(),
##   `Local Authority Code` = col_character(),
##   `Middle Layer Super Output Area (MSOA) Name` = col_character(),
##   `Middle Layer Super Output Area (MSOA) Code` = col_character(),
##   `Lower Layer Super Output Area (LSOA) Name` = col_character(),
##   `Lower Layer Super Output Area (LSOA) Code` = col_character(),
##   `Total number of domestic electricity meters` = col_double(),
##   `Total domestic electricity consumption (kWh)` = col_double(),
##   `Mean domestic electricity consumption 
## (kWh per meter)` = col_double(),
##   `Median domestic electricity consumption 
## (kWh per meter)` = col_double()
## )
domelec_LSOA_2019 <- readr::read_csv(paste0(dataFolder, "energy/electricity/LSOA Dom Elec csv/LSOA_ELEC_2019.csv"))
## 
## -- Column specification --------------------------------------------------------
## cols(
##   `Local Authority Name` = col_character(),
##   `Local Authority Code` = col_character(),
##   `Middle Layer Super Output Area (MSOA) Name` = col_character(),
##   `Middle Layer Super Output Area (MSOA) Code` = col_character(),
##   `Lower Layer Super Output Area (LSOA) Name` = col_character(),
##   `Lower Layer Super Output Area (LSOA) Code` = col_character(),
##   `Total number of domestic electricity meters` = col_double(),
##   `Total domestic electricity consumption (kWh)` = col_double(),
##   `Mean domestic electricity consumption 
## (kWh per meter)` = col_double(),
##   `Median domestic electricity consumption 
## (kWh per meter)` = col_double()
## )
domelec_LSOA_2010$de2010 <- domelec_LSOA_2010$'Mean domestic electricity consumption \n(kWh per meter)'
domelec_LSOA_2019$de2019 <- domelec_LSOA_2019$'Mean domestic electricity consumption \n(kWh per meter)'
# create unambiguous lsoa code for matching to each other and the sf geometry
domelec_LSOA_2010$LSOA11CD <- domelec_LSOA_2010$`Lower Layer Super Output Area (LSOA) Code`
domelec_LSOA_2019$LSOA11CD <- domelec_LSOA_2019$`Lower Layer Super Output Area (LSOA) Code`
  
merged_de <- merge(domelec_LSOA_2010, domelec_LSOA_2019, by="Lower Layer Super Output Area (LSOA) Code", all=TRUE)
names(merged_de)
##  [1] "Lower Layer Super Output Area (LSOA) Code"                  
##  [2] "Local Authority Name.x"                                     
##  [3] "Local Authority Code.x"                                     
##  [4] "Middle Layer Super Output Area (MSOA) Name.x"               
##  [5] "Middle Layer Super Output Area (MSOA) Code.x"               
##  [6] "Lower Layer Super Output Area (LSOA) Name.x"                
##  [7] "Total number of domestic electricity meters.x"              
##  [8] "Total domestic electricity consumption (kWh).x"             
##  [9] "Mean domestic electricity consumption \n(kWh per meter).x"  
## [10] "Median domestic electricity consumption \n(kWh per meter).x"
## [11] "de2010"                                                     
## [12] "LSOA11CD.x"                                                 
## [13] "Local Authority Name.y"                                     
## [14] "Local Authority Code.y"                                     
## [15] "Middle Layer Super Output Area (MSOA) Name.y"               
## [16] "Middle Layer Super Output Area (MSOA) Code.y"               
## [17] "Lower Layer Super Output Area (LSOA) Name.y"                
## [18] "Total number of domestic electricity meters.y"              
## [19] "Total domestic electricity consumption (kWh).y"             
## [20] "Mean domestic electricity consumption \n(kWh per meter).y"  
## [21] "Median domestic electricity consumption \n(kWh per meter).y"
## [22] "de2019"                                                     
## [23] "LSOA11CD.y"
merged_de$de_diff<-as.numeric(merged_de$de2019)-as.numeric(merged_de$de2010) 
summary(merged_de)
##  Lower Layer Super Output Area (LSOA) Code Local Authority Name.x
##  Length:41730                              Length:41730          
##  Class :character                          Class :character      
##  Mode  :character                          Mode  :character      
##                                                                  
##                                                                  
##                                                                  
##                                                                  
##  Local Authority Code.x Middle Layer Super Output Area (MSOA) Name.x
##  Length:41730           Length:41730                                
##  Class :character       Class :character                            
##  Mode  :character       Mode  :character                            
##                                                                     
##                                                                     
##                                                                     
##                                                                     
##  Middle Layer Super Output Area (MSOA) Code.x
##  Length:41730                                
##  Class :character                            
##  Mode  :character                            
##                                              
##                                              
##                                              
##                                              
##  Lower Layer Super Output Area (LSOA) Name.x
##  Length:41730                               
##  Class :character                           
##  Mode  :character                           
##                                             
##                                             
##                                             
##                                             
##  Total number of domestic electricity meters.x
##  Min.   :   21                                
##  1st Qu.:  544                                
##  Median :  652                                
##  Mean   :  652                                
##  3rd Qu.:  749                                
##  Max.   :48893                                
##                                               
##  Total domestic electricity consumption (kWh).x
##  Min.   :   103945                             
##  1st Qu.:  2168495                             
##  Median :  2580372                             
##  Mean   :  2704421                             
##  3rd Qu.:  3101820                             
##  Max.   :191175966                             
##                                                
##  Mean domestic electricity consumption \n(kWh per meter).x
##  Min.   : 1462                                            
##  1st Qu.: 3583                                            
##  Median : 3968                                            
##  Mean   : 4169                                            
##  3rd Qu.: 4497                                            
##  Max.   :13360                                            
##                                                           
##  Median domestic electricity consumption \n(kWh per meter).x     de2010     
##  Min.   :  250                                               Min.   : 1462  
##  1st Qu.: 2988                                               1st Qu.: 3583  
##  Median : 3335                                               Median : 3968  
##  Mean   : 3441                                               Mean   : 4169  
##  3rd Qu.: 3758                                               3rd Qu.: 4497  
##  Max.   :10533                                               Max.   :13360  
##                                                                             
##   LSOA11CD.x        Local Authority Name.y Local Authority Code.y
##  Length:41730       Length:41730           Length:41730          
##  Class :character   Class :character       Class :character      
##  Mode  :character   Mode  :character       Mode  :character      
##                                                                  
##                                                                  
##                                                                  
##                                                                  
##  Middle Layer Super Output Area (MSOA) Name.y
##  Length:41730                                
##  Class :character                            
##  Mode  :character                            
##                                              
##                                              
##                                              
##                                              
##  Middle Layer Super Output Area (MSOA) Code.y
##  Length:41730                                
##  Class :character                            
##  Mode  :character                            
##                                              
##                                              
##                                              
##                                              
##  Lower Layer Super Output Area (LSOA) Name.y
##  Length:41730                               
##  Class :character                           
##  Mode  :character                           
##                                             
##                                             
##                                             
##                                             
##  Total number of domestic electricity meters.y
##  Min.   :   43.0                              
##  1st Qu.:  560.0                              
##  Median :  670.0                              
##  Mean   :  688.1                              
##  3rd Qu.:  790.0                              
##  Max.   :47236.0                              
##  NA's   :3                                    
##  Total domestic electricity consumption (kWh).y
##  Min.   :   149948                             
##  1st Qu.:  1905454                             
##  Median :  2300776                             
##  Mean   :  2462117                             
##  3rd Qu.:  2850550                             
##  Max.   :146809165                             
##  NA's   :3                                     
##  Mean domestic electricity consumption \n(kWh per meter).y
##  Min.   : 1268                                            
##  1st Qu.: 3058                                            
##  Median : 3392                                            
##  Mean   : 3586                                            
##  3rd Qu.: 3848                                            
##  Max.   :14094                                            
##  NA's   :3                                                
##  Median domestic electricity consumption \n(kWh per meter).y     de2019     
##  Min.   : 806.3                                              Min.   : 1268  
##  1st Qu.:2530.4                                              1st Qu.: 3058  
##  Median :2814.3                                              Median : 3392  
##  Mean   :2902.6                                              Mean   : 3586  
##  3rd Qu.:3151.8                                              3rd Qu.: 3848  
##  Max.   :8731.5                                              Max.   :14094  
##  NA's   :3                                                   NA's   :3      
##   LSOA11CD.y           de_diff       
##  Length:41730       Min.   :-5873.3  
##  Class :character   1st Qu.: -711.7  
##  Mode  :character   Median : -553.2  
##                     Mean   : -582.8  
##                     3rd Qu.: -411.2  
##                     Max.   : 2456.0  
##                     NA's   :3
# merge to sf geometry on LSOA11CD
merged_de$LSOA11CD <- merged_de$"Lower Layer Super Output Area (LSOA) Code"
lsoa_sf_de20102019<-merge(lsoa_sf_data, merged_de)
ggplot2::ggplot(lsoa_sf_de20102019) +
  geom_sf(aes(fill=de_diff))+
  scale_fill_continuous(name="Change in Mean consumpton (kWh per meter)",low="green",high="red") +
  labs(caption="Solent(all LSOAs)")

lsoa_sf_de20102019$pc_diff <- 100*(lsoa_sf_de20102019$de_diff/lsoa_sf_de20102019$de2010)
t_diff <-  dplyr::select(as.data.frame(lsoa_sf_de20102019), 
                         LSOA11CD, `Lower Layer Super Output Area (LSOA) Name.y`,
                         de2010, de2019, de_diff, pc_diff)
head(arrange(t_diff, pc_diff))
##    LSOA11CD Lower Layer Super Output Area (LSOA) Name.y   de2010   de2019
## 1 E01023199                            Test Valley 013D 6265.640 3940.512
## 2 E01023201                            Test Valley 003B 4715.905 2992.417
## 3 E01022546                  Basingstoke and Deane 005D 5373.513 3416.407
## 4 E01017278                            Southampton 031D 3772.492 2439.883
## 5 E01023224                             Winchester 014A 6119.198 3960.261
## 6 E01022653                              Eastleigh 010A 5346.714 3549.718
##     de_diff   pc_diff
## 1 -2325.128 -37.10919
## 2 -1723.488 -36.54628
## 3 -1957.106 -36.42134
## 4 -1332.610 -35.32438
## 5 -2158.938 -35.28138
## 6 -1796.996 -33.60935
head(arrange(t_diff, -pc_diff))
##    LSOA11CD Lower Layer Super Output Area (LSOA) Name.y   de2010   de2019
## 1 E01017032                             Portsmouth 016B 3019.755 3296.955
## 2 E01032842                  Basingstoke and Deane 011E 4437.491 4738.937
## 3 E01022738                                Fareham 011D 2729.289 2891.538
## 4 E01022631                         East Hampshire 010D 6112.007 6375.912
## 5 E01022489                  Basingstoke and Deane 003B 7585.273 7845.624
## 6 E01022592                         East Hampshire 011A 7172.338 7403.228
##    de_diff  pc_diff
## 1 277.2005 9.179569
## 2 301.4457 6.793156
## 3 162.2488 5.944728
## 4 263.9044 4.317803
## 5 260.3506 3.432317
## 6 230.8898 3.219171
ggplot2::ggplot(t_diff, aes(x = pc_diff)) +
  geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

####Map with Overlay

library(leaflet)

st_coord_sys <- st_crs(lsoa_sf_de20102019)
st_coord_sys$epsg
## [1] 27700
#Transforming the coord system
if(st_coord_sys$epsg !=4326) {
  map_df_trans <- st_transform(lsoa_sf_de20102019, "+proj=longlat +datum=WGS84")
}
qpal <- colorNumeric("Reds", map_df_trans$de_diff, n=9)

leaflet(map_df_trans) %>% 
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addPolygons(color = ~qpal(de_diff),
              fillOpacity = 0.6, weight = 1.5, popup = ~(LSOA11CD ), 
             
  
               highlight = highlightOptions(
                weight = 5,
                color = "#666",
                fillOpacity = 0.7,
                bringToFront = TRUE))

#####Cities in more detail

#Cities such as Southampton disappear due to their density, we will now map Southampton alone to see this area in more detail
mapData <- dplyr::filter(lsoa_sf_de20102019, LAD11NM == "Southampton")
# plotting the electricity consumption of Southampton in more detail
ggplot2::ggplot(mapData) + 
  geom_sf(aes(fill= de_diff)) +
  scale_fill_continuous(name = "Change in Mean consumpton (kWh per meter)", low = "green", high = "red") +
  labs(caption = "Southampton")

#BROKEN
#Cities such as Portsmouth disappear due to their density, we will now map Portsmouth alone to see this area in more detail
mapData <- dplyr::filter(lsoa_sf_de20102019, LAD11NM == "Portsmouth")
# plotting the electricity consumption of Portsmouth in more detail
ggplot2::ggplot(mapData) + 
  geom_sf(aes(fill = de_diff)) +
  scale_fill_continuous(name = "Change in Mean comsumpton (kWh per meter)", low = "green", high = "red") +
  labs(caption = "Portsmouth")

#Cities such as Winchester disappear due to their density, we will now map Winchester alone to see this area in more detail
mapData <- dplyr::filter(lsoa_sf_de20102019, LAD11NM == "Winchester")
# plotting the electricity consumption of Winchester in more detail
ggplot2::ggplot(mapData) + 
  geom_sf(aes(fill = de_diff)) +
  scale_fill_continuous(name = "Change in Mean comsumpton (kWh per meter)", low = "green", high = "red") +
  labs(caption = "Winchester")

1.1.2 Mapping Gas

####2019

#Electricty consumption data LSOA pre downloaded
inFile <-paste0(dataFolder, "energy/gas/LSOA Gas csv/LSOA_GAS_2019.csv")
lsoa_gasData <- readr::read_csv(inFile)
## 
## -- Column specification --------------------------------------------------------
## cols(
##   `Local Authority Name` = col_character(),
##   `Local Authority Code` = col_character(),
##   `MSOA Name` = col_character(),
##   `Middle Layer Super Output Area (MSOA) Code` = col_character(),
##   `LSOA Name` = col_character(),
##   `Lower Layer Super Output Area (LSOA) Code` = col_character(),
##   `Number of consuming meters` = col_double(),
##   `Consumption (kWh)` = col_double(),
##   `Mean consumption (kWh per meter)` = col_double(),
##   `Median consumption (kWh per meter)` = col_double(),
##   `Number of non-consuming meters` = col_character()
## )
lsoa_gasData$LSOA11CD <- lsoa_gasData$`Lower Layer Super Output Area (LSOA) Code`
head(lsoa_gasData)
## # A tibble: 6 x 12
##   `Local Authority~ `Local Authority~ `MSOA Name` `Middle Layer Sup~ `LSOA Name`
##   <chr>             <chr>             <chr>       <chr>              <chr>      
## 1 Hartlepool        E06000001         Hartlepool~ E02002483          Hartlepool~
## 2 Hartlepool        E06000001         Hartlepool~ E02002483          Hartlepool~
## 3 Hartlepool        E06000001         Hartlepool~ E02002483          Hartlepool~
## 4 Hartlepool        E06000001         Hartlepool~ E02002483          Hartlepool~
## 5 Hartlepool        E06000001         Hartlepool~ E02002483          Hartlepool~
## 6 Hartlepool        E06000001         Hartlepool~ E02002483          Hartlepool~
## # ... with 7 more variables: Lower Layer Super Output Area (LSOA) Code <chr>,
## #   Number of consuming meters <dbl>, Consumption (kWh) <dbl>,
## #   Mean consumption (kWh per meter) <dbl>,
## #   Median consumption (kWh per meter) <dbl>,
## #   Number of non-consuming meters <chr>, LSOA11CD <chr>
lsoa_merged_sf_gas <- merge(lsoa_sf_data, lsoa_gasData) #merging the boundaries and energy data
ggplot2::ggplot(lsoa_merged_sf_gas) + 
  geom_sf(aes(fill = `Mean consumption (kWh per meter)`)) +
  scale_fill_continuous(name = "Gas: Mean kWh per meter", low = "green", high = "red") +
  labs(caption = "Solent (all LSOAs)")

#Mapping it

Aha - this one shows where there is no gas :-)

###Map with Overlay

library(leaflet)

st_coord_sys <- st_crs(lsoa_merged_sf_gas)
st_coord_sys$epsg 
## [1] 27700
#Transforming the coord system
if(st_coord_sys$epsg !=4326){
  map_df_trans <- st_transform(lsoa_merged_sf_gas, "+proj=longlat +datum=WGS84")
}
qpal <- colorNumeric("Reds", map_df_trans$`Mean consumption (kWh per meter)`, n=9)

leaflet(map_df_trans) %>%
  addTiles() %>%
  addPolygons(color= ~qpal(`Mean consumption (kWh per meter)`), 
              fillOpacity = 0.6, weight = 1.5, popup = ~(LSOA11CD),
              
              highlight=highlightOptions(
                weight=5,
                color="#666",
                fillOpacity = 0.7,
                bringToFront = TRUE))

#####Cities in More detail

#Cities such as Southampton disappear due to their density, we will now map Southampton alone to see this area in more detail
mapData <- dplyr::filter(lsoa_merged_sf_gas, LAD11NM == "Southampton")

# plotting the gas consumption of Southampton in more detail
ggplot2::ggplot(mapData) + 
  geom_sf(aes(fill = `Mean consumption (kWh per meter)`)) +
  scale_fill_continuous(name = "Gas: Mean kWh per meter", low = "green", high = "red") +
  labs(caption = "Southampton")

#Cities such as Portsmouth disappear due to their density, we will now map Portsmouth alone to see this area in more detail
mapData <- dplyr::filter(lsoa_merged_sf_gas, LAD11NM == "Portsmouth")

# plotting the gas consumption of Portsmouth in more detail
ggplot2::ggplot(mapData) + 
  geom_sf(aes(fill = `Mean consumption (kWh per meter)`)) +
  scale_fill_continuous(name = "Gas: Mean kWh per meter", low = "green", high = "red") +
  labs(caption = "Portsmouth")

#Cities such as Winchester disappear due to their density, we will now map Winchester alone to see this area in more detail
mapData <- dplyr::filter(lsoa_merged_sf_gas, LAD11NM == "Winchester")

# plotting the gas consumption of Winchester in more detail
ggplot2::ggplot(mapData) + 
  geom_sf(aes(fill = `Mean consumption (kWh per meter)`)) +
  scale_fill_continuous(name = "Gas: Mean kWh per meter", low = "green", high = "red") +
  labs(caption = "Winchester")

####Difference between 2010 and 2019 dom gas

#Green indicates a decrease in gas consumption from 2010 to 2019
#Red indicates an increase in gas consumption from 2010 to 2019
dgas_LSOA_2010 <- readr::read_csv(paste0(dataFolder, "energy/gas/LSOA Gas csv/LSOA_GAS_2010.csv"))
## 
## -- Column specification --------------------------------------------------------
## cols(
##   `Local Authority Name` = col_character(),
##   `Local Authority Code` = col_character(),
##   `MSOA Name` = col_character(),
##   `Middle Layer Super Output Area (MSOA) Code` = col_character(),
##   `LSOA Name` = col_character(),
##   `Lower Layer Super Output Area (LSOA) Code` = col_character(),
##   `Number of meters` = col_double(),
##   `Consumption (kWh)` = col_double(),
##   `Mean consumption (kWh per meter)` = col_double(),
##   `Median consumption (kWh per meter)` = col_double()
## )
dgas_LSOA_2019 <- readr::read_csv(paste0(dataFolder, "energy/gas/LSOA Gas csv/LSOA_GAS_2019.csv"))
## 
## -- Column specification --------------------------------------------------------
## cols(
##   `Local Authority Name` = col_character(),
##   `Local Authority Code` = col_character(),
##   `MSOA Name` = col_character(),
##   `Middle Layer Super Output Area (MSOA) Code` = col_character(),
##   `LSOA Name` = col_character(),
##   `Lower Layer Super Output Area (LSOA) Code` = col_character(),
##   `Number of consuming meters` = col_double(),
##   `Consumption (kWh)` = col_double(),
##   `Mean consumption (kWh per meter)` = col_double(),
##   `Median consumption (kWh per meter)` = col_double(),
##   `Number of non-consuming meters` = col_character()
## )
dgas_LSOA_2010$dgas2010 <- dgas_LSOA_2010$'Mean consumption (kWh per meter)'
dgas_LSOA_2019$dgas2019 <- dgas_LSOA_2019$'Mean consumption (kWh per meter)'
merged_dgas <- merge(dgas_LSOA_2010, dgas_LSOA_2019, by = "Lower Layer Super Output Area (LSOA) Code", all = TRUE)
names(merged_dgas)
##  [1] "Lower Layer Super Output Area (LSOA) Code"   
##  [2] "Local Authority Name.x"                      
##  [3] "Local Authority Code.x"                      
##  [4] "MSOA Name.x"                                 
##  [5] "Middle Layer Super Output Area (MSOA) Code.x"
##  [6] "LSOA Name.x"                                 
##  [7] "Number of meters"                            
##  [8] "Consumption (kWh).x"                         
##  [9] "Mean consumption (kWh per meter).x"          
## [10] "Median consumption (kWh per meter).x"        
## [11] "dgas2010"                                    
## [12] "Local Authority Name.y"                      
## [13] "Local Authority Code.y"                      
## [14] "MSOA Name.y"                                 
## [15] "Middle Layer Super Output Area (MSOA) Code.y"
## [16] "LSOA Name.y"                                 
## [17] "Number of consuming meters"                  
## [18] "Consumption (kWh).y"                         
## [19] "Mean consumption (kWh per meter).y"          
## [20] "Median consumption (kWh per meter).y"        
## [21] "Number of non-consuming meters"              
## [22] "dgas2019"
merged_dgas$dg_diff<-as.numeric(merged_dgas$dgas2019) - merged_dgas$dgas2010
summary(merged_dgas)
##  Lower Layer Super Output Area (LSOA) Code Local Authority Name.x
##  Length:40226                              Length:40226          
##  Class :character                          Class :character      
##  Mode  :character                          Mode  :character      
##                                                                  
##                                                                  
##                                                                  
##                                                                  
##  Local Authority Code.x MSOA Name.x       
##  Length:40226           Length:40226      
##  Class :character       Class :character  
##  Mode  :character       Mode  :character  
##                                           
##                                           
##                                           
##                                           
##  Middle Layer Super Output Area (MSOA) Code.x LSOA Name.x       
##  Length:40226                                 Length:40226      
##  Class :character                             Class :character  
##  Mode  :character                             Mode  :character  
##                                                                 
##                                                                 
##                                                                 
##                                                                 
##  Number of meters  Consumption (kWh).x Mean consumption (kWh per meter).x
##  Min.   :    6.0   Min.   :    56289   Min.   :  553.6                   
##  1st Qu.:  475.0   1st Qu.:  6614181   1st Qu.:12997.8                   
##  Median :  596.0   Median :  8662085   Median :14915.0                   
##  Mean   :  566.2   Mean   :  8581876   Mean   :15444.4                   
##  3rd Qu.:  673.0   3rd Qu.: 10475081   3rd Qu.:17241.8                   
##  Max.   :67568.0   Max.   :931766817   Max.   :52729.2                   
##  NA's   :104       NA's   :104         NA's   :104                       
##  Median consumption (kWh per meter).x    dgas2010       Local Authority Name.y
##  Min.   :    1                        Min.   :  553.6   Length:40226          
##  1st Qu.:11957                        1st Qu.:12997.8   Class :character      
##  Median :13870                        Median :14915.0   Mode  :character      
##  Mean   :14290                        Mean   :15444.4                         
##  3rd Qu.:16058                        3rd Qu.:17241.8                         
##  Max.   :56900                        Max.   :52729.2                         
##  NA's   :104                          NA's   :104                             
##  Local Authority Code.y MSOA Name.y       
##  Length:40226           Length:40226      
##  Class :character       Class :character  
##  Mode  :character       Mode  :character  
##                                           
##                                           
##                                           
##                                           
##  Middle Layer Super Output Area (MSOA) Code.y LSOA Name.y       
##  Length:40226                                 Length:40226      
##  Class :character                             Class :character  
##  Mode  :character                             Mode  :character  
##                                                                 
##                                                                 
##                                                                 
##                                                                 
##  Number of consuming meters Consumption (kWh).y
##  Min.   :    5.0            Min.   :1.860e+04  
##  1st Qu.:  492.0            1st Qu.:6.166e+06  
##  Median :  615.0            Median :8.062e+06  
##  Mean   :  599.2            Mean   :8.087e+06  
##  3rd Qu.:  705.0            3rd Qu.:9.839e+06  
##  Max.   :87991.0            Max.   :1.018e+09  
##  NA's   :13                 NA's   :13         
##  Mean consumption (kWh per meter).y Median consumption (kWh per meter).y
##  Min.   : 1193                      Min.   :  537.1                     
##  1st Qu.:11476                      1st Qu.:10361.9                     
##  Median :13197                      Median :12045.5                     
##  Mean   :13768                      Mean   :12531.2                     
##  3rd Qu.:15384                      3rd Qu.:14090.4                     
##  Max.   :47003                      Max.   :49456.0                     
##  NA's   :13                         NA's   :13                          
##  Number of non-consuming meters    dgas2019        dg_diff      
##  Length:40226                   Min.   : 1193   Min.   :-22666  
##  Class :character               1st Qu.:11476   1st Qu.: -2090  
##  Mode  :character               Median :13197   Median : -1601  
##                                 Mean   :13768   Mean   : -1676  
##                                 3rd Qu.:15384   3rd Qu.: -1143  
##                                 Max.   :47003   Max.   : 23242  
##                                 NA's   :13      NA's   :117
merged_dgas$LSOA11CD <- merged_dgas$"Lower Layer Super Output Area (LSOA) Code" #creating a variable with the LSOA code in the same name as in sf_data
lsoa_dgas20102019 <- merge(lsoa_sf_data, merged_dgas) #merging these
ggplot2::ggplot(lsoa_dgas20102019) + geom_sf(aes(fill=dg_diff))+ 
                  scale_fill_continuous(name="Change in Gas: Mean kWh per meter", low="green",high="red")+
  labs(caption ="Solent (all MSOAs)") ##BROKEN

##Need to change the varibale names to reflect what is actually there

####Map with overlay

library(leaflet)

st_coord_sys <- st_crs(lsoa_dgas20102019)
st_coord_sys$epsg
## [1] 27700
#Transforming the coor system
if(st_coord_sys$epsg !=4326){
  map_df_trans <- st_transform(lsoa_dgas20102019, "+proj=longlat +datum=WGS84")
}
qpal <- colorNumeric("Reds", map_df_trans$dg_diff, n=9)

leaflet(map_df_trans) %>%
  addTiles() %>%
  addPolygons(color = ~qpal(dg_diff),
              fillOpacity = 0.6, weight = 1.5, popup = ~(LSOA11CD), 
             
  
               highlight = highlightOptions(
                weight = 5,
                color = "#666",
                fillOpacity = 0.7,
                bringToFront = TRUE))

#####Cities in more detail

#Cities such as Southampton disappear due to their density, we will now map Southampton alone to see this area in more detail
mapData <- dplyr::filter(lsoa_dgas20102019, LAD11NM == "Southampton")

# plotting the gas consumption of Southampton in more detail
ggplot2::ggplot(mapData) + 
  geom_sf(aes(fill = dg_diff)) +
  scale_fill_continuous(name = "Gas: Mean kWh per meter", low = "green", high = "red") +
  labs(caption = "Southampton")

#Cities such as Portsmouth disappear due to their density, we will now map Portsmouth alone to see this area in more detail
mapData <- dplyr::filter(lsoa_dgas20102019, LAD11NM == "Portsmouth")

# plotting the gas consumption of Portsmouth in more detail
ggplot2::ggplot(mapData) + 
  geom_sf(aes(fill = dg_diff)) +
  scale_fill_continuous(name = "Gas: Mean kWh per meter", low = "green", high = "red") +
  labs(caption = "Portsmouth")

#Cities such as Winchester disappear due to their density, we will now map Winchester alone to see this area in more detail
mapData <- dplyr::filter(lsoa_dgas20102019, LAD11NM == "Winchester")

# plotting the gas consumption of Winchester in more detail
ggplot2::ggplot(mapData) + 
  geom_sf(aes(fill = dg_diff)) +
  scale_fill_continuous(name = "Gas: Mean kWh per meter", low = "green", high = "red") +
  labs(caption = "Winchester")